package common

import (
	"fmt"
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
)

/*const Field_Tid string = "tenant_id"
const Field_MiniAppId string = "mini_app_id"
const Field_Uid string = "uid"
const Field_Nil string = ""*/

type WhoField struct {
    Tid WhoFieldType
    MiniAppId WhoFieldType
    Uid WhoFieldType
}

type WhoFieldType string

const (
	Field_Tid WhoFieldType = "tenant_id"
	Field_MiniAppId WhoFieldType = "mini_app_id"
	Field_Uid WhoFieldType = "uid"
)


var WhoCol = &WhoField{
				Tid: Field_Tid,
				MiniAppId: Field_MiniAppId,
				Uid: Field_Uid,
			}

func (this *Who) GetFilter(fields...WhoFieldType) *xorm.Session {
	if len(fields) > 3 {
		panic("fields's len must be less than or equal to 3")
	}

	mapfield := make(map[string]interface{})
	for _, v := range fields {
		if v == Field_Tid {
			mapfield["tenant_id"] = this.TenantId
		} else if v == Field_MiniAppId {
			mapfield["mini_app_id"] = this.MiniAppId
		} else if v == Field_Uid {
			mapfield["uid"] = this.Uid
		} else {
			panic(fmt.Errorf("not support WhoFiled(%s)", v))
		}
	}

	s := lego.GetDBEngine().Where("if_del = ?", 0).And(mapfield)
	return s
}
